package fr.xgouchet.wedding.client.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

import fr.xgouchet.wedding.client.i18n.Constants;
import fr.xgouchet.wedding.client.presenter.AgendaPresenter.Display;
import fr.xgouchet.wedding.shared.model.Task;

/**
 * UI Widget for Tasks List
 * 
 * @author x.gouchet
 * 
 */
public class TasksListView extends Composite implements Display {

	/**
	 * Default Constructor
	 */
	public TasksListView() {
		// main panel
		VerticalPanel main = new VerticalPanel();
		initWidget(main);

		// top buttons panel
		HorizontalPanel topBar = new HorizontalPanel();
		topBar.addStyleName("tasksTopBar");

		// create buttons
		mAddButton = new Button(Constants.I18N.buttonAdd());
		mDeleteButton = new Button(Constants.I18N.buttonDelete());
		mCategoryFilters = new ListBox();
		mCompleteFilters = new ListBox();
		mCompleteFilters.addItem(Constants.I18N.labelShowAllTasks(), "");
		mCompleteFilters.addItem(Constants.I18N.labelShowOpenTasks(), "open");
		mCompleteFilters.addItem(Constants.I18N.labelShowClosedTasks(),
				"closed");
		mCompleteFilters.addItem(Constants.I18N.labelShowOverdueTasks(),
				"overdue");

		// create list
		mList = new FlexTable();
		mList.addStyleName("tasksList");

		// final assembly
		topBar.add(mAddButton);
		topBar.add(mCompleteFilters);
		topBar.add(mCategoryFilters);
		topBar.add(mDeleteButton);
		main.add(topBar);
		main.add(mList);

	}

	/**
	 * @see Display#getAddButton()
	 */
	public HasClickHandlers getAddButton() {
		return mAddButton;
	}

	/**
	 * @see Display#getDeleteButton()
	 */
	public HasClickHandlers getDeleteButton() {
		return mDeleteButton;
	}

	/**
	 * @see Display#getSelectedRows()
	 */
	public ArrayList<Integer> getSelectedRows() {
		ArrayList<Integer> selectedRows = new ArrayList<Integer>();

		for (int i = 0; i < mList.getRowCount(); ++i) {
			CheckBox checkBox = (CheckBox) mList.getWidget(i, 0);
			if ((checkBox != null) && (checkBox.getValue()))
				selectedRows.add(i - 1);
		}

		return selectedRows;
	}

	/**
	 * @see Display#getCategoryFilters()
	 */
	public HasChangeHandlers getCategoryFilters() {
		return mCategoryFilters;
	}

	/**
	 * @see Display#getCompleteFilters()
	 */
	public HasChangeHandlers getCompleteFilters() {
		return mCompleteFilters;
	}

	/**
	 * @see Display#getCategoryFilterValue()
	 */
	public String getCategoryFilterValue() {
		return mCategoryFilters.getValue(mCategoryFilters.getSelectedIndex());
	}

	/**
	 * @see Display#getCompleteFilterValue()
	 */
	public String getCompleteFilterValue() {
		return mCompleteFilters.getValue(mCompleteFilters.getSelectedIndex());
	}

	/**
	 * @see Display#clearData()
	 */
	public void clearData() {
		mList.removeAllRows();
		mList.setText(0, 1, Constants.I18N.labelTasks());
		mList.setText(0, 2, "Date");
		mList.setText(0, 3, "");

	}

	/**
	 * @see Display#addData(Task)
	 */
	public HasClickHandlers addData(final Task task) {
		int row = mList.getRowCount();

		DisclosurePanel content = new DisclosurePanel(task.getTitle());
		content.add(new Label(task.getDescription()));

		mList.setWidget(row, 0, new CheckBox());
		mList.setWidget(row, 1, content);

		String ds;
		if (task.getDaysBeforeWedding() > 45)
			ds = (new DateBox.DefaultFormat(DateTimeFormat.getFormat("MM/yyyy"))
					.format(null, task.getDeadline()));
		else
			ds = (new DateBox.DefaultFormat(DateTimeFormat
					.getFormat(Constants.I18N.dateFormat())).format(null, task
					.getDeadline()));

		mList.setWidget(row, 2, new Label(ds));

		Button edit = new Button(Constants.I18N.buttonEdit());
		mList.setWidget(row, 3, edit);

		if (task.isComplete())
			mList.getRowFormatter().addStyleName(row, "taskComplete");
		else if (task.getDeadline().before(new Date()))
			mList.getRowFormatter().addStyleName(row, "taskOverDue");

		return edit;
	}

	/**
	 * @see Display#setFilters(ArrayList)
	 */
	public void setFilters(ArrayList<String> filters) {
		mCategoryFilters.clear();
		Iterator<String> it = filters.iterator();
		mCategoryFilters.addItem(Constants.I18N.labelFilters(), "");
		while (it.hasNext()) {
			String filter = it.next();
			mCategoryFilters.addItem(filter.toUpperCase(), filter);
		}
	}

	/**
	 * @see Display#getClickedRow(ClickEvent)
	 */
	public int getClickedRow(ClickEvent event) {
		int selectedRow = -1;
		HTMLTable.Cell cell = mList.getCellForEvent(event);

		if (cell != null) {
			// ignore if the user not on the button
			if (cell.getCellIndex() == 3)
				selectedRow = cell.getRowIndex() - 1;
		}
		return selectedRow;
	}

	/**
	 * @see Display#asWidget()
	 */
	public Widget asWidget() {
		return this;
	}

	/** add task button */
	private Button mAddButton;

	/** delete task button */
	private Button mDeleteButton;

	/** Category Filters */
	private ListBox mCategoryFilters;

	/** Ignore the complete tasks */
	private ListBox mCompleteFilters;

	/** Tasks List */
	private FlexTable mList;

}
